#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using Castle.Windsor;
using Flynn.Core;
using Flynn.Core.Tasks;
using Flynn.TaskServices;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Tests
{
    [TestFixture]
    public class ConsoleRunnerFixture
    {
        private MockRepository _mocks;

        private Engine _engine;
        private IWindsorContainer _container;
        private ConsoleRunner _consoleRunner;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _engine = new Engine(_mocks.Stub<IProjectLoader>(), _mocks.Stub<ITaskExecutionService>());

            _container = new WindsorContainer();
            _consoleRunner = new ConsoleRunner(_engine, _container);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Engine_Must_Not_Be_Null()
        {
            new ConsoleRunner(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Executing_Args_Must_Not_Be_Null()
        {
            _consoleRunner.Execute(null);
        }

        [Test]
        public void Task_Must_Be_Specified()
        {
            string result = _consoleRunner.Execute(new string[] {});

            Assert.AreEqual("Failure: No task specified", result);
        }

        [Test]
        public void Only_Known_Task_Must_Be_Specified()
        {
            string result = _consoleRunner.Execute(new string[] {"/task:UnknownTask"});

            Assert.AreEqual("Failure: Can't execute task 'UnknownTask'", result);
        }

        [Test]
        public void Can_Execute()
        {
            ITaskService taskService = _mocks.CreateMock<ITaskService>();

            _container.Kernel.AddComponentInstance("MyTask", typeof(ITaskService), taskService);

            using (_mocks.Record())
            {
                Expect.Call(taskService.Execute(_engine, new string[] {"/arg1", "/arg2"})).Return("Result output");
            }

            using (_mocks.Playback())
            {
                string result = _consoleRunner.Execute(new string[] {"/task:MyTask", "/arg1", "/arg2"});

                Assert.AreEqual("Result output", result);
            }
        }
    }
}